<?php
namespace app\admin\controller;
use think\Controller;
// use app\admin\model\AdminUserModel;

class Index extends Controller
{
    public function index(){
        $admin_user =  session('admin_user');
        // dump($uid);exit;
        if(empty($admin_user)){
            return $this->login();
        }

        $this->assign('admin_user',$admin_user);
        return $this->fetch();

    }

    public function test(){
        dump(md5(123456));
    }

    public function store()
    {
        $data = db('store')->paginate(15);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();

    }

    public function add()
    {
        return $this->fetch();
    }


    /**
     * 数据导出页面
     * @param string $value [description]
     */
    public function export_html($value='')
    {

        return $this->fetch();
    }


    public function export($value='')
    {

        $param = input('param.');
        $store_db = db('store');
        $seconds_db = db('seconds');
        
        $where = [];
        if((!empty($param['m']))){
            $where = array_merge($where,['y'=>$param['y']]);
            $y = $param['y'];
           
        }else{
            $y = '';
        }
        if((!empty($param['m']))){
            $where = array_merge($where,['m'=>$param['m']]);
            $m = $param['m'];
            
        }else{
            $m = '';
        }
        if((!empty($param['d']))){
            $where = array_merge($where,['d'=>$param['d']]);
            $d = $param['d'];
           
        }else{
            $d = '';
        }
        session('y',$y);
        session('m',$m);
        session('d',$d);

        $store_date = $store_db->select();
        

        foreach ($store_date as $k => $v) {
            $count = $seconds_db->where($where)->where(['key'=>$v['id']])->count();
            $data[$k]['region'] = $v['region'];
            $data[$k]['name'] = $v['name'];
            $data[$k]['type'] = $v['type'];
            $data[$k]['address'] = $v['address'];
            $data[$k]['count'] = $count;
        }
        
        
        $this->assign('data',$data);
        
        return $this->fetch();
    }
    

    

  
    

   

    //订单下载
    public function export1(){
        $y = session('y');
        $m = session('m');
        $d = session('d');
        $where = [];
        if((!empty($y))){
            $where = array_merge($where,['y'=>$y]);
            
           
        }
        if((!empty($m))){
            $where = array_merge($where,['m'=>$m]);
            
            
        }
        if((!empty($d))){
            $where = array_merge($where,['d'=>$d]);
            
           
        }

        $store_db = db('store');
        $seconds_db = db('seconds');
        $store_date = $store_db->select();
        foreach ($store_date as $k => $v) {
            $count = $seconds_db->where($where)->where(['key'=>$v['id']])->count();
            $data[$k]['region'] = $v['region'];
            $data[$k]['name'] = $v['name'];
            $data[$k]['type'] = $v['type'];
            $data[$k]['address'] = $v['address'];
            $data[$k]['count'] = $count;
        }

        $xlsData = $data;

        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        // $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');//如果报错Class 'XMLWriter' not found,则用下面一行
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
 
        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H");
        $arrHeader = array('辖区','门店','类型','地址','年份','月份','日','人数');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };

        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['region']);
            $objActSheet->setCellValue('B'.$k, $v['name']);
            // // 图片生成
            // $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            // $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
            // // 设置宽度高度
            // $objDrawing[$k]->setHeight(40);//照片高度
            // $objDrawing[$k]->setWidth(40); //照片宽度
            // /*设置图片要插入的单元格*/
            // $objDrawing[$k]->setCoordinates('C'.$k);
            // // 图片偏移距离
            // $objDrawing[$k]->setOffsetX(30);
            // $objDrawing[$k]->setOffsetY(12);
            // $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            // 表格内容
            $objActSheet->setCellValue('C'.$k, $v['type']);
            $objActSheet->setCellValue('D'.$k, $v['address']);
            $objActSheet->setCellValue('E'.$k, $y);
            $objActSheet->setCellValue('F'.$k, $m);
            $objActSheet->setCellValue('G'.$k, $d);
            $objActSheet->setCellValue('H'.$k, $v['count']);
            
            
            
            
 
 
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(20,20,15,10,10,30,10,15);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[5]);
        $objActSheet->getColumnDimension('B')->setWidth($width[1]);
        $objActSheet->getColumnDimension('C')->setWidth($width[0]);
        $objActSheet->getColumnDimension('D')->setWidth($width[5]);
        $objActSheet->getColumnDimension('E')->setWidth($width[5]);
        $objActSheet->getColumnDimension('F')->setWidth($width[5]);
        $objActSheet->getColumnDimension('G')->setWidth($width[5]);
        $objActSheet->getColumnDimension('H')->setWidth($width[5]);
        $objActSheet->getColumnDimension('I')->setWidth($width[5]);
 
        
        $outfile = "订单表.xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
    


 





    /**
     * 登录界面
     */
    public function login(){
        return view('login/index');
    }

    /**
     * 登录验证
     */
    public function do_login(){
        if(request()->isPost()){
            $post = input('post.');
            
            if(!empty($post['account'] && !empty($post['password']))){
                $data = db('admin_user')->where(['account'=>$post['account'],'password'=>md5($post['password'])])->find();
                if($data){
                    session('admin_user',$data);
                    return $this->redirect('Index/index');
                }else{
                    return $this->error('账号或密码不正确');
                }
            }else{
                return $this->error('账号密码不能为空');
            }
            
        }
    }



    /**
     * 修改密码
     */
    public function edit(){
        
        $uid =  session('admin_user');
        if(empty($uid)){
            return $this->login();
        }
        if(request()->isPost()){
            $param = input('param.');
            
            $pwd = db('admin_user')->where(['id'=>1])->value('password');
            if(md5($param['old_pwd']) == $pwd){
                if(md5($param['new_pwd']) == $pwd){
                   return $this->error('新密码不能与原密码一样'); 
                }
                if($param['new_pwd'] == $param['re_new_pwd']){
                    $num = db('admin_user')->where(['id'=>1])->update(['password'=>md5($param['new_pwd'])]);
                    // dump($num);exit;
                    if($num>0){
                        session('admin_user_id',null);
                        return $this->success('成功','Index/login');
                    }else{
                      return $this->error('失败');
                    }
                }else{
                    return $this->error('两次密码不一致');
                }
            }else{
                return $this->error('原密码不正确');
            }
        }
        return $this->fetch();
    }



    /**
     * 用户退出
     */
    public function login_out(){
        session('admin_user',null);
        return $this->login();
    }

}
